// pages/order/order.js

Page({
  /**
   * 页面的初始数据
   */
  data: {
    shopList:[],
    shopNum:0,
    money:'0',
    num:0,
    checkedIds:[], // 选中的id列表,
    checkedAll:false
  },
  checkboxChange(e){
    this.data.shopList.filter((item)=>{
      if(item.id=== e.currentTarget.dataset.id){
        item.isCheck = !item.isCheck
      }
    })
    console.log(this.data.shopList);
    wx.request({
      url: 'http://localhost:6060/checkAll',
      method: "POST",//指定请求方式，默认get
      data: {
        id: e.currentTarget.dataset.id,
        isCheck: this.data.shopList[e.currentTarget.dataset.index].isCheck
      },
      header: {
         //默认值'Content-Type': 'application/json'
        'content-type': 'application/x-www-form-urlencoded' //post
      },
      success: function (res) {
        console.log(res.data)
      }
    });
    
      var nnu = this.data.shopList.map((item)=>{
        if(item.isCheck){
          return item.price * item.num
        }else{
          return 0
        }
      }).reduce((p,n)=>{
              return Number(p)+Number(n)
      },0)
      var nuu =  this.data.shopList.filter((item)=>{
        if(item.isCheck){
          return item
        }
      }).length
      this.setData({
              money:nnu,
              shopNum:nuu
       })
    var quan =  this.data.shopList.length
    console.log(quan);
    var two = this.data.shopList.filter((item)=>{
      if(item.isCheck){
        return item
      }
    }).length
    console.log(two);
    if(quan === two){
      this.setData({checkedAll:true})
    }else{
      this.setData({checkedAll:false})
    }
  },
  // 计算
  moneyAll:function(){
    var nnu = this.data.shopList.map((item)=>{
      if(item.isCheck){
        return item.price * item.num
      }else{
        return 0
      }
    }).reduce((p,n)=>{
            return Number(p)+Number(n)
    },0)
    var nuu =  this.data.shopList.filter((item)=>{
      if(item.isCheck){
        return item
      }
    }).length
    this.setData({
            money:nnu,
            shopNum:nuu
     })
  },
  //全选
  selectAll(e){
    console.log(this.data.checkedAll);
    var checkedAll = !this.data.checkedAll
    console.log(checkedAll);
    if(checkedAll){
      this.setData({checkedAll})
      this.data.shopList.forEach((item)=>{
        var itemId = item.id
        wx.request({
          url: 'http://localhost:6060/checkAll',
          method: "POST",//指定请求方式，默认get
          data: {
            id: itemId,
            isCheck: checkedAll
          },
          header: {
             //默认值'Content-Type': 'application/json'
            'content-type': 'application/x-www-form-urlencoded' //post
          },
          success: function (res) {
            console.log(res.data)
          }
        });
      })
      this.onLoad()
      console.log('全选');
      console.log(this.data.shopList);
      var nnu = this.data.shopList.map((item)=>{
          return item.price * item.num
      }).reduce((p,n)=>{
              return Number(p)+Number(n)
      },0)
      var nuu =  this.data.shopList.length
      console.log(nnu,nuu);
      this.setData({
        money:nnu,
        shopNum:nuu
 })
    }else{
      this.setData({checkedAll:false})
      this.data.shopList.forEach((item)=>{
        wx.request({
          url: 'http://localhost:6060/checkAll',
          method: "POST",//指定请求方式，默认get
          data: {
            id: item.id,
            isCheck: false
          },
          header: {
             //默认值'Content-Type': 'application/json'
            'content-type': 'application/x-www-form-urlencoded' //post
          },
          success: function (res) {
            console.log(res.data)
          }
        });
      })
      this.onLoad()
      
      this.setData({
        money:0,
            shopNum:0
      })
    }
    this.onLoad()
   
    
  },
  open: function(e){
    var jiesuanData = this.data.shopList.filter((item)=>{
      if(item.isCheck){
        return item
      }
    })
    console.log(jiesuanData);
    wx.navigateTo({
      url: '/pages/jiesuan/jiesuan',
      events: {
        // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
        acceptDataFromOpenedPage: function(data) {
          console.log(data)
        },
        someEvent: function(data) {
          console.log(data)
        }
      },
      success: function(res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromOpenerPage', { data: jiesuanData })
      }
    })
  },
 
  //加加
  add: function(e){
    this.data.shopList.filter((item)=>{
      if(item.id === e.currentTarget.dataset.id){
        item.num++
      }
    })
    wx.request({
      url: 'http://localhost:6060/addBtnList',
      method: "POST",//指定请求方式，默认get
      data: {
        id: e.currentTarget.dataset.id,
        num: this.data.shopList[e.currentTarget.dataset.index].num
      },
      header: {
         //默认值'Content-Type': 'application/json'
        'content-type': 'application/x-www-form-urlencoded' //post
      },
      success: function (res) {
        console.log(res.data)
      }
    });

    this.onLoad()
    var sefl = this
     wx.request({
            url: 'http://localhost:6060/shopList', // 仅为示例，需填写自己服务器的地址
            data: {},
            header: {//请求头
              'content-type': 'application/json' // 默认值
            },
            method:"GET",
            success(res) {
              var list = res.data.map((item)=>{
                return {
                  id:item.id,
                  name:item.shopName,
                  img:item.img,
                  num:Number(item.num),
                  label:item.label,
                  isCheck:JSON.parse(item.isCheck),
                  price:item.price,
                  state:item.state
                }
              })
              sefl.setData({
                shopList:list
              })
              console.log(res.data);      //打印到控制台
            }
          })
      var nnu = this.data.shopList.map((item)=>{
        if(item.isCheck){
          return item.price * item.num
        }else{
          return 0
        }
      }).reduce((p,n)=>{
              return Number(p)+Number(n)
      },0)
      var nuu =  this.data.shopList.filter((item)=>{
        if(item.isCheck){
          return item
        }
      }).length
      this.setData({
              money:nnu,
              shopNum:nuu
       })
  },
  //减减
  dist:function(e){
    var flag = true
    if(this.data.shopList[e.currentTarget.dataset.index].num<=1){
      flag = false
    }
    if(flag){
      this.data.shopList.filter((item)=>{
        if(item.id === e.currentTarget.dataset.id){
          item.num--
        }
      })
      wx.request({
        url: 'http://localhost:6060/jianjian',
        method: "POST",//指定请求方式，默认get
        data: {
          id: e.currentTarget.dataset.id,
          num: this.data.shopList[e.currentTarget.dataset.index].num
        },
        header: {
           //默认值'Content-Type': 'application/json'
          'content-type': 'application/x-www-form-urlencoded' //post
        },
        success: function (res) {
          console.log(res.data)
        }
      });
      this.onLoad()
      var sefl = this
       wx.request({
              url: 'http://localhost:6060/shopList', // 仅为示例，需填写自己服务器的地址
              data: {},
              header: {//请求头
                'content-type': 'application/json' // 默认值
              },
              method:"GET",
              success(res) {
                var list = res.data.map((item)=>{
                  return {
                    id:item.id,
                    name:item.shopName,
                    img:item.img,
                    num:Number(item.num),
                    label:item.label,
                    isCheck:JSON.parse(item.isCheck),
                    price:item.price,
                    state:item.state
                  }
                })
                sefl.setData({
                  shopList:list
                })
                console.log(res.data);      //打印到控制台
              }
            })
    }
    var nnu = this.data.shopList.map((item)=>{
      if(item.isCheck){
        return item.price * item.num
      }else{
        return 0
      }
    }).reduce((p,n)=>{
            return Number(p)+Number(n)
    },0)
    var nuu =  this.data.shopList.filter((item)=>{
      if(item.isCheck){
        return item
      }
    }).length
    this.setData({
            money:nnu,
            shopNum:nuu
     })
  },
  makeRequest: function() {
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var sefl = this
     wx.request({
            url: 'http://localhost:6060/shopList', // 仅为示例，需填写自己服务器的地址
            data: {},
            header: {//请求头
              'content-type': 'application/json' // 默认值
            },
            method:"GET",
            success(res) {
              var list = res.data.map((item)=>{
                return {
                  id:item.id,
                  name:item.shopName,
                  img:item.img,
                  num:Number(item.num),
                  label:item.label,
                  isCheck:JSON.parse(item.isCheck),
                  price:item.price,
                  state:item.state
                }
              })
              sefl.setData({
                shopList:list
              })
              console.log(res.data);      //打印到控制台
            }
          })
      
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log(1);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})